package com.example.demo;

import com.example.demo.model.User;
import org.springframework.http.HttpRequest;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.*;
import java.util.Arrays;
import java.util.List;


@RestController
public class HelloController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello SpringBoot 中文";
    }

    @RequestMapping("/info")
    public String info(String name) {
        return name;
    }

    @RequestMapping("/info1")
    public String info(String name,String age) {
        return "name: " + name + ", age: " + age;
    }

    @RequestMapping("/info2")
    public String info(User user) {
        return user.toString();
    }

    @RequestMapping("/info3")
    public String info3(String[] str) {
        return Arrays.toString(str);
    }

    @RequestMapping("/info4")
    public String info4(@RequestParam(required = false) List<String> str) {
        return str.toString();
    }

    @RequestMapping("/info5")
    public String info5(@RequestBody User user) {
        return user.toString();
    }


    @RequestMapping("/info6")
    public String info6(@CookieValue("cookie1") String cookieValue) {
        return "cookie : " + cookieValue;
    }

    @RequestMapping("/info13")
    public String info13(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        }
        return "cookie获取完毕";
    }




    @RequestMapping("/setSession")
    public String setSession(HttpSession session) {
        session.setAttribute("name","zhangsan");
        return "session set ok";
    }



    @RequestMapping("/info8")
    public String info8(@RequestPart MultipartFile file) {
        return file.getOriginalFilename();
    }

    @RequestMapping("/info9")
    public String info9(@RequestHeader("host") String host) {
        return "host : " + host;
    }

    @RequestMapping("/info10")
    public String info10(HttpServletRequest request ) {
        String host = request.getHeader("host");
        return "host : " + host;
    }


    @RequestMapping("/info7")
    public String info7(@SessionAttribute("name") String name) {
        return "name: "+ name;
    }

    @RequestMapping("/info11")
    public String info11(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        return "name: " + name;
    }

    @RequestMapping("/info12")
    public String info12(HttpSession session) {
        String name = (String) session.getAttribute("name");
        return "name: " + name;
    }


}
